//! 内核汇编及异常入口

pub use semx_entry::main_entry;

use crate::{
    irq::irq_init,
    processor::{nr_cpus, set_this_processor_id, NR_CPUS},
    sched::task::{task_init, uaccess::THREAD_SIZE},
    space::kalloc::{kalloc_init, kmalloc, GfpFlags},
};

#[cfg(target_arch = "aarch64")]
mod aarch64;

/// IRQ 栈
#[no_mangle]
pub static mut IRQ_STACK_PTR: [usize; NR_CPUS] = [0; NR_CPUS];

#[allow(clippy::needless_range_loop)]
fn irq_stack_init() {
    for cpu in 0..nr_cpus() {
        unsafe {
            IRQ_STACK_PTR[cpu] = kmalloc(THREAD_SIZE, GfpFlags::ZERO_NORMAL).unwrap();
        }
    }
}

#[doc(hidden)]
#[export_name = "__main_setup_early"]
pub unsafe fn __main_setup_early(f: fn() -> !) {
    // 设置处理器标识 id
    set_this_processor_id(0);

    // 初始化控制台
    extern "Rust" {
        fn __console_init();
    }
    unsafe {
        __console_init();
    }

    kalloc_init();
    irq_stack_init();
    task_init();
    irq_init();

    f();
}
